//
//  HomeHDClassificationVC.m
//  jingdongfang
//
//  Created by LJL on 2018/5/11.
//  Copyright © 2018年 BOE. All rights reserved.
//

#import "HomeHDClassificationVC.h"
#import "FilterReusableView.h"
#import "FilterCell.h"
#import "HotClassDetailVC.h"
#import "HotClassCell_first.h"
#import "HotClassCell_second.h"
#import "HomeHDTypeVC.h"
#import "IGArtLibraryApi.h"

@interface HomeHDClassificationVC ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate>
{
    NSArray *titleArr;
}
@property (nonatomic , strong) IGArtLibraryApi * artLibraryApi;
@property (strong, nonatomic) NSMutableArray *dataSource;
@property (nonatomic, strong)UICollectionView *collectionV;

@end

@implementation HomeHDClassificationVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self creatNav];
    [self loadData];
    [self loadUI];
    [self dateRequest];
}

- (void)creatNav
{
    self.title = NSLocalizedString(@"home17", nil);
}

- (void)loadData
{
    self.artLibraryApi = [IGArtLibraryApi new];
    self.artLibraryApi.isCreatListLabel = YES;
    self.dataSource = [NSMutableArray array];
}

- (void)dateRequest
{
    [self.artLibraryApi startWithDecodeClass:[IGArtLibraryResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
            IGArtLibraryResp *resp = responseObject;
            IGArtLibraryModel *model = resp.data;
            _dataSource = [NSMutableArray arrayWithArray:model.lstClass];
            [_collectionV reloadData];
        }
    }];
}

- (void)loadUI
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT) collectionViewLayout:layout];
    self.collectionV.backgroundColor = [UIColor c3Color];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.collectionV registerNib:[UINib nibWithNibName:@"FilterCell" bundle:nil] forCellWithReuseIdentifier:@"FilterCell"];
    [self.collectionV registerNib:[UINib nibWithNibName:@"HotClassCell_first" bundle:nil] forCellWithReuseIdentifier:@"HotClassCell_first"];
    [self.collectionV registerNib:[UINib nibWithNibName:@"HotClassCell_second" bundle:nil] forCellWithReuseIdentifier:@"HotClassCell_second"];
    
    [self.collectionV registerNib:[UINib nibWithNibName:@"FilterReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FilterReusableView"];
    [self.collectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];
    [self.view addSubview:self.collectionV];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataSource.count ? self.dataSource.count : 0;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    IGArtLibrary *lib = self.dataSource[section];
    return self.dataSource.count ? [lib.lstSubClass count] : 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataSource && self.dataSource.count > 0)
    {
        IGArtLibrary *lib = self.dataSource[indexPath.section];
        IGArtDetailLibrary *class = lib.lstSubClass[indexPath.row];
        HotClassCell_second *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotClassCell_second" forIndexPath:indexPath];
        cell.textL.text = class.title;
        return cell;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataSource && self.dataSource.count > 0)
    {
        return CGSizeMake((self.view.width - 60)/3, 35);
    }
    return CGSizeZero;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        IGArtLibrary *lib = self.dataSource[indexPath.section];
        FilterReusableView *reusableV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FilterReusableView" forIndexPath:indexPath];
        if (!reusableV)
        {
            reusableV = [[NSBundle mainBundle] loadNibNamed:@"FilterReusableView" owner:self options:nil].firstObject;
        }
        reusableV.typeL.text = lib.title;
        reusableV.moreBtn.hidden = YES;
        return reusableV;
    } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
    UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter" forIndexPath:indexPath];
    if (!footer)
    {
        footer = [[UICollectionReusableView alloc] init];
    }
    footer.backgroundColor = [UIColor c2Color];
    return footer;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    IGArtLibrary *lib = self.dataSource[section];
    if (lib.lstSubClass.count > 0)
    {
        return CGSizeMake(KSCREENWIDTH, 60);
    }
    return CGSizeZero;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    IGArtLibrary *lib = self.dataSource[section];
    if (lib.lstSubClass.count > 0)
    {
        return CGSizeMake(KSCREENWIDTH, 10);
    }
    return CGSizeZero;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionVeliew layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (self.dataSource && self.dataSource.count > 0)
    {
        return UIEdgeInsetsMake(5, 20, 5, 20);
    }
    return UIEdgeInsetsZero;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGArtLibrary *lib = self.dataSource[indexPath.section];
    IGArtDetailLibrary *class = lib.lstSubClass[indexPath.row];
    HomeHDTypeVC *vc = [[HomeHDTypeVC alloc] init];
    vc.type =  class.aId;
    vc.title = class.title;
    [UIViewController ig_pushViewController:vc animated:YES];
}

//cell的最小列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (self.dataSource && self.dataSource.count > 0)
    {
        return 10;
    }
    return 0;
}

//cell的最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (self.dataSource && self.dataSource.count > 0)
    {
        return 5;
    }
    return 0;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



@end
